package com.earnings.okhttputils.utils.https;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.earnings.okhttputils.utils.OkHttp.callback.Callback;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.User;
import com.earnings.okhttputils.utils.bean.BaseBean;
import com.earnings.okhttputils.utils.utils.CommonUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by HOHO on 2017/4/25.
 */

public abstract class HttpObjectCallback<T> extends Callback<String> {

    private Context context;
    public HttpObjectCallback(Context context){
        this.context=context;
    }
    @Override
    public String parseNetworkResponse(Response response, int id) throws Exception {

        Log.e(Constant.TAG,response.request().url().toString() );
        return response.body().string();
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        e.printStackTrace();
       Toast.makeText( context, Constant.MSG_ERROR ,Toast.LENGTH_SHORT).show();
        onFail(Constant.STATUS_ERROR,Constant.MSG_ERROR);
    }

    @Override
    public void onResponse(String response, int id) {
        Log.e(Constant.TAG,response.toString() );
        try {
        JsonGenericsSerializator json=new JsonGenericsSerializator();
        Gson gson =new Gson();

       // response
        BaseBean entity = json.transform(response,BaseBean.class);
        if (Constant.STATUS_SUCCESS==entity.getStatus()) {
            T   object = null;
            Class <T> entityClass = (Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];//相当于T.class
            if(entity.getResult()!=null){
            if(entityClass==JSONObject.class)object= (T)new JSONObject(entity.getResult().toString());
            else if(entityClass==String.class){
                if(entity.getResult().toString().length()>1)
                object= (T)entity.getResult().toString().substring(1,entity.getResult().toString().length()-1);
                else object=(T)entity.getResult().toString();
            }
            else object = gson.fromJson(entity.getResult(),entityClass);
           //     Log.e(Constant.TAG,entity.getResult().toString().substring(1,entity.getResult().toString().length()-1) );
            }


            onSuccess(object,entity.getMsg());
        }else {
            if(entity.getStatus()==Constant.STATUS_TOKEN) {//token不正确
                Toast.makeText( context, "您的账号已在其它地方登录，请重新登录", Toast.LENGTH_SHORT ).show();
                User user= CommonUtil.getUser();
                user.setUser_id( "" );
                user.setToken( "" );
                CommonUtil.commitUser( user );
            }

            onFail(entity.getStatus(),entity.getMsg());
        }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public abstract void onSuccess(T object,String msg);
    public abstract void onFail(int code,String msg);
}
